import os

from utils.PathUtil import RESULT_PATH
from utils.time_utils import get_dateToSecond, YMDHMS
import numpy as np

# 摄像机/门铃 开流结果
def generate_play_result(file_path):
    print('\n====================结果统计====================')
    file_name = os.path.join(RESULT_PATH, file_path)
    with open(file_name, mode='r', encoding='utf-8') as f:
        l = f.readlines()
        timeSum = 0
        count, successCound, errCount, err_not_get, err_not_send, err_app_not_show = 0, 0, 0, 0, 0, 0
        list_time = []

        maxTime, minTime = 0, 100
        for item in l:

            if '开始时间' in item:
                count += 1
            if '开流失败' in item:
                errCount += 1

            if '耗时：' in item:
                t = float(item.split('耗时：')[1])
                list_time.append(t)
                timeSum = timeSum + t
                successCound += 1
                if t > maxTime:
                    maxTime = t
                if t < minTime:
                    minTime = t

        print('总次数：', count, '\n')
        print('失败次数：', errCount)
        print('失败率：', str(round((errCount / count) * 100, 2)) + '%')
        if successCound > 0:
            print('成功次数：', successCound)
            print('成功率：', str(round((successCound / count) * 100, 2)) + '%')

            print('平均耗时：', round(timeSum / successCound, 2))
            print('最大耗时：', round(maxTime, 2))
            print('最小耗时：', round(minTime, 2))
            print('方差：', np.var(list_time))
    print('====================结果统计====================')


# 摄像机/门铃 绑定结果
def genrate_bind_result(file_path):
    print('\n====================结果统计====================')
    file_name = os.path.join(RESULT_PATH, file_path)
    with open(file_name, mode='r', encoding='utf-8') as f:
        l = f.readlines()
        timeSum = 0
        count, successCound, errCount, err_not_get, err_not_send, err_app_not_show = 0, 0, 0, 0, 0, 0

        maxTime, minTime = 0, 100
        for item in l:

            if '====' in item:
                count += 1
            if '失败' in item:
                errCount += 1

            if '耗时：' in item:
                t = float(item.split('耗时：')[1])
                timeSum = timeSum + t
                successCound += 1
                if t > maxTime:
                    maxTime = t
                if t < minTime:
                    minTime = t

        print('总次数：', count, '\n')
        print('失败次数：', errCount)
        print('失败率：', str(round((errCount / count) * 100, 2)) + '%')

        print('成功次数：', successCound)
        print('成功率：', str(round((successCound / count) * 100, 2)) + '%')
        if successCound>0:
            print('平均耗时：', round(timeSum / successCound, 2))
        print('最大耗时：', round(maxTime, 2))
        print('最小耗时：', round(minTime, 2))
    print('====================结果统计====================')


# OTA结果
def generate_lock_ota_result(file_path):
    print('\n====================结果统计====================')
    file_name = os.path.join(RESULT_PATH, file_path)

    with open(file_name, mode='r', encoding='utf-8') as f:
        l = f.readlines()
        timeSum = 0
        count, successCound, errCount = 0, 0, 0
        maxTime, minTime = 0, 100

        start_upgrade_time, end_upgrade_time = 0, 0
        for item in l:

            if '分割线' in item:
                count += 1
                start_upgrade_time, end_upgrade_time = 0, 0
            if '升级异常：' in item or '升级失败' in item:
                errCount += 1
            if 'App已点击"马上升级"' in item:
                start_upgrade_time = get_dateToSecond(item.split('.')[0], YMDHMS)
            if 'OTA升级成功' in item:
                successCound += 1
                end_upgrade_time = get_dateToSecond(item.split('.')[0], YMDHMS)
                utime = int(end_upgrade_time) - int(start_upgrade_time)
                timeSum += utime

                if utime > maxTime:
                    maxTime = utime
                if utime < minTime:
                    minTime = utime

        print('总次数：', count, '\n')
        print('失败次数：', errCount)
        print('失败率：', str(round((errCount / count) * 100, 2)) + '%')

        print('成功次数：', successCound)
        print('成功率：', str(round((successCound / count) * 100, 2)) + '%')
        if successCound > 0:
            print('平均耗时：', round(timeSum / successCound, 2))
        print('最大耗时：', round(maxTime, 2))
        print('最小耗时：', round(minTime, 2))
    print('====================结果统计====================')
